package com.deyuanyun.pic.ctrl.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.deyuanyun.pic.ctrl.controller.dto.exinsp.PipeBaseQuery;
import com.deyuanyun.pic.ctrl.controller.vo.defect.CoatingStatisticsVO;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.deyuanyun.pic.common.util.NumberUtil;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.ctrl.service.CpTestDashboardExpandService;
import com.deyuanyun.pic.pipe.controller.BaseController;

/**
 *  仪表板Controller
 *
 * @author ChenKui
 * @date 2016-06-16
 */
@Controller
@RequestMapping("/corrctrl/CpTestDashboard")
public class CpTestDashboardController extends BaseController{

    private @Resource CpTestDashboardExpandService cpTestDashboardExpandService;

    /**
     * 进去仪表板
     *
     * @param model
     */
    @RequestMapping("/initCpTest.html")
    public ModelAndView initCpTest(ModelMap model) {
        return forword("/corrctrl/dashboard/cathodicProtect", model);
    }

    
    /**
     * 查询维修任务列表
     * 
     * @param pipeLineId
     * @param model
     */
    @RequestMapping("/queryCpTestDashboardRatio.json")
    @ResponseBody
    public void queryCpTestDashboardRatio(@RequestParam("pipeLineId")String pipeLineId
            ,@RequestParam("beginMileage") BigDecimal beginMileage
            ,@RequestParam("endMileage") BigDecimal endMileage
            ,ModelMap model) {

        //运行率
        Map<String,BigDecimal> cpTestStationOperatingRatioMap = cpTestDashboardExpandService.queryCpTestStationOperatingRatio(pipeLineId, beginMileage, endMileage);
        if(cpTestStationOperatingRatioMap == null || cpTestStationOperatingRatioMap.isEmpty()){
            cpTestStationOperatingRatioMap = new HashMap<String, BigDecimal>();
            cpTestStationOperatingRatioMap.put("AVG_OPERATINGRATIO", null);
        }
        model.addAttribute("cpTestStationOperatingRatioMap", cpTestStationOperatingRatioMap);
        
        //保护率
        Integer beginMileage_int = NumberUtil.toIntegerDown(beginMileage, 3);
        Integer endMileage_int = NumberUtil.toIntegerUp(endMileage, 3);
        Map<String,BigDecimal> cpTestStationProtectionRatioMap = cpTestDashboardExpandService.queryCpTestStationProtectionRatio(pipeLineId, beginMileage_int, endMileage_int);
        model.addAttribute("cpTestStationProtectionRatioMap", cpTestStationProtectionRatioMap);
        
        
        //电位情况
        List<Map<String,Object>> cpTestStationRecodeList = cpTestDashboardExpandService.queryCpTestStationRecode(pipeLineId, beginMileage_int, endMileage_int);
        model.addAttribute("cpTestStationRecodeList", cpTestStationRecodeList);
        
        
        AjaxSupport.sendSuccessText("success", model);
        //return forword("/pbf/queryRepairTaskList.jsp", model);
    }

    /**
     * 仪表板 涂层统计
     *
     * @param pipeBaseQuery
     */
    @RequestMapping("/coatingStatistics.json")
    @ResponseBody
    public void coatingStatistics(PipeBaseQuery pipeBaseQuery) {

        CoatingStatisticsVO coatingStatisticsVO = new CoatingStatisticsVO();
        coatingStatisticsVO.setAcvgList(cpTestDashboardExpandService.queryExinspPcmAcvg(pipeBaseQuery));
        coatingStatisticsVO.setDcvgList(cpTestDashboardExpandService.queryExinspCipsDcvg(pipeBaseQuery));
        coatingStatisticsVO.setCoatingGradeList(cpTestDashboardExpandService.queryCoatingGrade(pipeBaseQuery));

        AjaxSupport.sendSuccessText("success", coatingStatisticsVO);
    }

}
